package com.clj.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.clj.entity.StudentDO_clj;
import com.clj.service.StudentService_clj;
import com.clj.service.impl.StudentServiceCljImpl_clj;
import com.clj.view.MainView_clj;
import com.clj.view.UpdateStudentView_clj;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 16:00
 * Description: 修改按钮的监听器
 */
public class UpdateStudentViewHandler_clj implements ActionListener {
    private UpdateStudentView_clj updateStudentViewClj;
    private MainView_clj mainViewClj;

    public UpdateStudentViewHandler_clj(UpdateStudentView_clj updateStudentViewClj, MainView_clj mainViewClj) {
        this.updateStudentViewClj = updateStudentViewClj;
        this.mainViewClj = mainViewClj;
    }

    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            StudentService_clj studentServiceClj = new StudentServiceCljImpl_clj();
            StudentDO_clj studentDOClj = updateStudentViewClj.buildUpdatedStudentDO();
            boolean updateResult = studentServiceClj.update(studentDOClj);
            if (updateResult) {
                //重新加载表格，添加最新数据
                mainViewClj.reloadTable();
                updateStudentViewClj.dispose();
            } else {
                JOptionPane.showMessageDialog(updateStudentViewClj, "修改失败");
            }
        }
    }
}
